package team.gs.io;

public class myIO {

	public myIO() {
	}

	public static byte readByte(Message message) {
		byte bt = message.getBuffer().readByte();
		// System.out.println("READBYTE: "+bt);
		return bt;
	}

	public static short readShort(Message message) {
		short word0 = 0;
		for (int i = 0; i < 2; i++) {
			word0 <<= 8;
			word0 |= 0xff & message.getBuffer().readByte();
		}
		// System.out.println("WORD: "+word0);
		return word0;
	}

	public static byte[] readBytes(Message message) {
		int i = readInt(message);
		return message.getBuffer().readBytes(i);
	}

	public static long readLong(Message message) {
		long l = 0L;
		for (int i = 0; i < 8; i++) {
			l <<= 8;
			l |= 0xff & message.getBuffer().readByte();
		}
		// System.out.println("READLONG: "+l);
		return l;
	}

	public static int readInt(Message message) {
		int i = 0;
		for (int j = 0; j < 4; j++) {
			i <<= 8;
			i |= 0xff & message.getBuffer().readByte();
		}
		// System.out.println("READINT: "+i);
		return i;
	}

	public static boolean readBoolean(Message message) {
		byte byte0 = message.getBuffer().readByte();
		// System.out.println("READBOOLEAN: "+byte0);
		return byte0 == 1;
	}

	public static char readChar(Message message) {
		short word0 = readShort(message);
		// System.out.println("READCHAR: "+(char)word0);
		return (char) word0;
	}

	public static String readString(Message message) {
		short word0 = readShort(message);
		StringBuffer stringbuffer = new StringBuffer(word0);
		for (int i = 0; i < word0; i++)
			stringbuffer.append(readChar(message));
		// System.out.println("READSTRING: "+stringbuffer.toString());
		return stringbuffer.toString();
	}

	public static String[] readArrayString(Message message) {
		short word0 = readShort(message);
		String as[] = new String[word0];
		for (int i = 0; i < word0; i++)
			as[i] = readString(message);

		return as;
	}

	public static int[] readArrayInt(Message message) {
		short word0 = readShort(message);
		int ai[] = new int[word0];
		for (int i = 0; i < word0; i++)
			ai[i] = readInt(message);

		return ai;
	}

	public static int[] readArrayByte(Message message) {
		short word0 = readShort(message);
		int ai[] = new int[word0];
		for (int i = 0; i < word0; i++)
			ai[i] = readByte(message);

		return ai;
	}

	public static void writeArrayInt(int ai[], Message message) {
		writeShort((short) ai.length, message);
		for (int i = 0; i < ai.length; i++)
			writeInt(ai[i], message);

	}

	public static void writeArrayByte(int ai[], Message message) {
		writeShort((short) ai.length, message);
		for (int i = 0; i < ai.length; i++)
			writeByte(ai[i], message);

	}

	public static void write(byte abyte0[], int i, int j, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(j + 2);
		writeShort(j, message);
		message.getBuffer().writeBytes(abyte0, i, j);
	}

	public static void writeByte(int i, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(1);
		message.getBuffer().writeByte((byte) i);
	}

	public static void writeBoolean(boolean flag, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(1);
		if (flag)
			message.getBuffer().writeByte((byte) 1);
		else
			message.getBuffer().writeByte((byte) 0);
	}

	public static void writeInt(int i, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(4);
		for (int j = 3; j >= 0; j--)
			message.getBuffer().writeByte((byte) (i >> j * 8));

	}

	public static void writeShort(short word0, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(2);
		for (int i = 1; i >= 0; i--)
			message.getBuffer().writeByte((byte) (word0 >> i * 8));

	}

	public static void writeShort(int i, Message message) {
		writeShort((short) i, message);
	}

	public static void writeLong(long l, Message message) {
		BufferData bufferdata = message.getBuffer();
		bufferdata.ensureAvail(8);
		for (int i = 7; i >= 0; i--)
			message.getBuffer().writeByte((byte) (int) (l >> i * 8));

	}

	public static void writeChar(char c, Message message) {
		short word0 = (short) c;
		writeShort(word0, message);
	}

	public static void writeString(String s, Message message) {
		if (s == null)
			s = "";
		BufferData bufferdata = message.getBuffer();
		int i = s.length();
		bufferdata.ensureAvail(4 + 2 * i);
		writeShort((short) i, message);
		for (int j = 0; j < i; j++) {
			short word0 = (short) s.charAt(j);
			writeShort(word0, message);
		}

	}

	public static void writeArrayString(String as[], Message message) {
		writeShort((short) as.length, message);
		for (int i = 0; i < as.length; i++)
			writeString(as[i], message);

	}

	public static byte[] int2Bytes(int i) {
		byte abyte0[] = new byte[4];
		for (int j = 3; j >= 0; j--) {
			abyte0[j] = (byte) i;
			i >>= 8;
		}

		return abyte0;
	}

	public static int bytes2Int(byte abyte0[]) {
		int i = 0;
		for (int j = 0; j < 4; j++) {
			i <<= 8;
			i |= 0xff & abyte0[j];
		}

		return i;
	}
}